package com.surent.controller.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hailong.surent.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.surent.view.custom.toast.AppToast;

/**
 * 
 * @author whailong [wanghl@ncsi.com.cn]
 * @date 16 Jun 2014 10:46:52
 */
@ContentView(R.layout.aty_main)
public class AtyMain extends FragmentActivity {

	@ViewInject(R.id.llt_navi0to3)
	private LinearLayout lltnavi0to3;

	@ViewInject(R.id.llt_naviBarHouse)
	private LinearLayout lltBarHouse;

	@ViewInject(R.id.llt_naviBarChat)
	private LinearLayout lltBarChat;

	@ViewInject(R.id.llt_naviBarMine)
	private LinearLayout lltBarMine;

	@ViewInject(R.id.iv_mainHouse)
	private ImageView ivHouse;

	@ViewInject(R.id.iv_mainChat)
	private ImageView ivChat;

	@ViewInject(R.id.iv_mainMine)
	private ImageView ivMine;

	@ViewInject(R.id.tv_mainHouse)
	private TextView tvHouse;

	@ViewInject(R.id.tv_mainChat)
	private TextView tvChat;

	@ViewInject(R.id.tv_mainMine)
	private TextView tvMine;

	private Fragment[] mFragments;
	private int currentFrag;
	private Menu menu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		initFragment(0);
		setIndicator(0);
		// House house = new House();
		// house.ininData("title", 1, 100, "payType", "houseLayout", 20, 1, 3,
		// "orientation", "decorateLevel",
		// "detailInfo", "communityName", new AVGeoPoint(1, 1), "linkName",
		// "linkPhoneNum");
		// house.saveInBackground();
	}

	/**
	 * 初始化fragment
	 */
	private void initFragment(int whichIsDefault) {

		mFragments = new Fragment[3];
		mFragments[0] = getSupportFragmentManager().findFragmentById(R.id.fragment_house);
		mFragments[1] = getSupportFragmentManager().findFragmentById(R.id.fragment_chat);
		mFragments[2] = getSupportFragmentManager().findFragmentById(R.id.fragment_mine);

		getSupportFragmentManager().beginTransaction().hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2])
				.show(mFragments[whichIsDefault]).commit();
	}

	@OnClick({ R.id.llt_naviBarHouse, R.id.llt_naviBarChat, R.id.llt_naviBarMine })
	void navigationBarOnclick(View view) {
		AppToast.makeText(this, "sdfsdf", AppToast.CONFIRM);
		switch (view.getId()) {
		case R.id.llt_naviBarHouse:
			setIndicator(0);
			break;
		case R.id.llt_naviBarChat:
			setIndicator(1);
			break;
		case R.id.llt_naviBarMine:
			setIndicator(2);
			break;

		}
	}

	/**
	 * 设置nevigation bar，谁被选中
	 * 
	 * @param which
	 */
	public void setIndicator(int which) {
		currentFrag = which;
		switch (which) {
		case 0:
			if (menu != null) {
				menu.removeItem(R.menu.frag_chum);
				getMenuInflater().inflate(R.menu.frag_house, menu);
			}
			getSupportFragmentManager().beginTransaction().hide(mFragments[1]).hide(mFragments[2]).show(mFragments[0])
					.commit();
			// 设置选中按钮的状态
			ivHouse.setImageResource(R.drawable.tab_house_select);
			tvHouse.setTextColor(Color.parseColor("#4a8e0b"));
			// 清除以前选中按钮的状态
			ivChat.setImageResource(R.drawable.tab_chum_normal);
			tvChat.setTextColor(Color.parseColor("#8b8b8b"));
			ivMine.setImageResource(R.drawable.tab_mine_normal);
			tvMine.setTextColor(Color.parseColor("#8b8b8b"));
			break;
		case 1:
			if (menu != null)
				getMenuInflater().inflate(R.menu.frag_chum, menu);
			getSupportFragmentManager().beginTransaction().hide(mFragments[0]).hide(mFragments[2]).show(mFragments[1])
					.commit();
			ivChat.setImageResource(R.drawable.tab_chum_select);
			tvChat.setTextColor(Color.parseColor("#4a8e0b"));

			ivHouse.setImageResource(R.drawable.tab_house_normal);
			tvHouse.setTextColor(Color.parseColor("#8b8b8b"));
			ivMine.setImageResource(R.drawable.tab_mine_normal);
			tvMine.setTextColor(Color.parseColor("#8b8b8b"));
			break;
		case 2:
			getSupportFragmentManager().beginTransaction().hide(mFragments[0]).hide(mFragments[1]).show(mFragments[2])
					.commit();

			ivMine.setImageResource(R.drawable.tab_mine_select);
			tvMine.setTextColor(Color.parseColor("#4a8e0b"));
			ivChat.setImageResource(R.drawable.tab_chum_normal);
			tvChat.setTextColor(Color.parseColor("#8b8b8b"));
			ivHouse.setImageResource(R.drawable.tab_house_normal);
			tvHouse.setTextColor(Color.parseColor("#8b8b8b"));
			break;
		default:
			break;
		}
	}

}
